package org.example.deadlock;

import lombok.extern.slf4j.Slf4j;
import org.example.utils.Sleeper;

/**
 * @author zhengdp
 * @Description 死锁测试
 * @Date 2022/10/30 18:41
 * @Created by zhengdp
 */
@Slf4j(topic = "c.DeadLockTest")
public class DeadLockTest {
    public static void main(String[] args) {
        Object A = new Object();
        Object B = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (A) {
                log.info("get Lock A ");
                Sleeper.sleep(1000);
                synchronized (B){
                    log.info(" get Lock B");
                    log.info("execute operation ....");
                }
            }
        },"t1");

        Thread t2 = new Thread(() -> {
            synchronized (B) {
                log.info("get Lock B ");
                Sleeper.sleep(500);
                synchronized (A){
                    log.info(" get Lock A");
                    log.info("execute operation ....");
                }
            }
        },"t2");

        t1.start();
        t2.start();
    }
}
